package com.woniuxy.creditsystem.common.mapper;

import com.woniuxy.creditsystem.common.bean.CustomerInfo;
import com.woniuxy.creditsystem.common.bean.VehicleFind;
import com.woniuxy.creditsystem.common.bean.VehicleInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.Date;
import java.util.List;


/**
 * @author ：luoyue
 * @date ：Created in 2020/12/10
 * @description：车辆表mapper
 * @modified By：
 * @version: 1.0v
 */
@Mapper
public interface VehicleMapper {


    /**
     * 新增车辆信息
     *
     * @param vehicleInfo
     * @return 受影响的行数
     * @author luoyue
     */
    @Options(
            useGeneratedKeys = true,
            keyProperty = "v.id"
    )
    @Insert("insert into vehicle_info " +
            "(brands,car_model,vehicle_license,frame_number,engine_number,car_color,vehicle_years,bare_car_price,buy_time,car_license_time,driven_distance,car_condition,vehicle_use,city,car_appearance,car_interior,car_engine,car_chassis,car_other,fk_customer_id)" +
            " values(#{v.brands},#{v.carModel},#{v.vehicleLicense},#{v.frameNumber},#{v.engineNumber},#{v.carColor},#{v.vehicleYears},#{v.bareCarPrice},#{v.buyTime},#{v.carLicenseTime},#{v.drivenDistance},#{v.carCondition},#{v.vehicleUse},#{v.city},#{v.carAppearance},#{v.carInterior},#{v.carEngine},#{v.carChassis},#{v.carOther},#{v.customerInfo.id})")
    int saveVehicleInfo(@Param("v") VehicleInfo vehicleInfo);



    /**
     * 根据ID查询车辆
     *
     * @param id
     * @return
     * @author luoyue
     */
    @Results({
            @Result(id = true, property = "id", column = "id", javaType = Long.class),
            @Result(property = "brands", column = "brands", javaType = String.class),
            @Result(property = "carModel", column = "car_model", javaType = String.class),
            @Result(property = "vehicleLicense", column = "vehicle_license", javaType = String.class),

            @Result(property = "frameNumber", column = "frame_number", javaType = String.class),
            @Result(property = "engineNumber", column = "engine_number", javaType = String.class),
            @Result(property = "carColor", column = "car_color", javaType = String.class),

            @Result(property = "vehicleYears", column = "vehicle_years", javaType = int.class),
            @Result(property = "bareCarPrice", column = "bare_car_price", javaType = Double.class),
            @Result(property = "buyTime", column = "buy_time", javaType = Date.class),

            @Result(property = "carLicenseTime", column = "car_license_time", javaType = Date.class),
            @Result(property = "drivenDistance", column = "driven_distance", javaType = Double.class),
            @Result(property = "carCondition", column = "car_condition", javaType = String.class),

            @Result(property = "vehicleUse", column = "vehicle_use", javaType = String.class),
            @Result(property = "city", column = "city", javaType = String.class),


            @Result(property = "customerInfo", column = "fk_customer_id", javaType = CustomerInfo.class, one =
            @One(select = "com.woniuxy.creditsystem.common.mapper.CustomerMapper.getOneById", fetchType = FetchType.LAZY)),
    })
    @Select("select * from vehicle_info where id = #{id}")
    VehicleInfo getOneById(Long id);


    /**
     * 根据参数进行分页查询
     *
     * @param
     * @param vehicleFind 条件
     * @return
     * @author luoyue
     */
    List<VehicleInfo> findAllByParams( @Param("vf") VehicleFind vehicleFind);


    /**
     * 修改车辆信息
     *v.
     * @param vehicleInfo
     * @return 受影响的行数
     */
    @Update("<script>" +
            "update vehicle_info" +
            "<set>" +
            "<if test='v.brands != null'>" +
            " brands = #{v.brands}," +
            "</if>" +
            "<if test='v.vehicleLicense != null'>" +
            " vehicle_license = #{v.vehicleLicense}," +
            "</if>" +
            "<if test='v.carColor != null'>" +
            " car_color = #{v.carColor}," +
            "</if>" +
            "<if test='v.vehicleYears != null'>" +
            " vehicle_years = #{v.vehicleYears}," +
            "</if>" +
            "<if test='v.carCondition != null'>" +
            " car_condition = #{v.carCondition}," +
            "</if>" +
            "</set>" +
            "where id = #{v.id}" +
            "</script>")
    int updateVehicle(@Param("v") VehicleInfo vehicleInfo);


    /**
     * 删除车辆信息
     * @param id
     * @return 受影响的行数
     */

    @Delete("delete from vehicle_info where id = #{id}")
    int deleteVehicle(@Param("id")Long id);

}
